package com.wyp168.leetcode;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName MaximumUnitsSolution
 * @Title TODO
 * @Description TODO
 * @Author wyp168
 * @Date 2022/11/15 08:19
 */
public class MaximumUnitsSolution {
    public static void main(String[] args) {
        int[][] boxTypes = {{1, 3}, {2, 2}, {3, 1}};
        int truckSize = 4;
        MaximumUnitsSolution solution = new MaximumUnitsSolution();
        int result = solution.maximumUnits(boxTypes, truckSize);
        System.out.println(result);
    }

    public int maximumUnits(int[][] boxTypes, int truckSize) {
        List<int[]> types = Arrays.asList(boxTypes);

        types = types.stream().sorted((a, b) -> b[1] - a[1]).collect(Collectors.toList());

        int result = 0;
        int index = 0;
        while (truckSize > 0) {
            int[] ints = types.get(index);
            if (ints[0] == 0) {
                index ++;
            } else {
                ints[0] --;
                result += ints[1];
                truckSize --;
            }
        }
        return result;
    }
}
